package com.ktjy.ktmall.dao.impl;

import com.ktjy.ktmall.dao.BaseDao;
import com.ktjy.ktmall.dao.CartItemDao;
import com.ktjy.ktmall.entity.CartItem;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @version v1.0
 * @Author 郴州科泰
 * @Create 2024-08-01 - 20:27
 */
public class CartItemDaoImpl extends BaseDao implements CartItemDao {

   /**
   * 加入购物车
   * @param cartItem 购物车对象
   */
   @Override
    public int insert(CartItem cartItem) {
        String sql = "insert into `t_cart`(`user_id`,`sku_id`,`goods_count`) values(?,?,?)";
        return super.executeUpdate(sql, cartItem.getUserId(),cartItem.getSkuId(),cartItem.getGoodsCount());
    }

    /**
     * 根据微信用户id和skuId查询购物车详情(用于判断当前用户是否重复购买此商品)
     * 如果是重复购买，就对购买数量进行累加。
     * @param userId 微信用户id
     * @param skuId 商品sku的id
     */
    @Override
    public CartItem findByUserIdAndSkuId(int userId, int skuId) {
        CartItem c = null;
        String sql = "select * from `t_cart` where `user_id` = ? and `sku_id` = ?";
        rs = super.executeQuery(sql, userId, skuId);
        try {
            if (rs.next()) {
                c = new CartItem(rs.getInt("id"), rs.getInt("user_id"), rs.getInt("sku_id"), rs.getInt("goods_count"), rs.getInt("selected"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return c;
    }

    /**
     * 根据微信用户id和skuId对重复加入购物车的商品进行购买数量累加
     * @param userId 用户id
     * @param skuId 商品sku的id
     * @param goodsCount 本次加入购物车的购买数量
     */
    @Override
    public int updateBuyNum(int userId, int skuId, int goodsCount) {
        String sql = "update `t_cart` set `goods_count` = `goods_count` + ? where `user_id` = ? AND `sku_id` = ?";
        return super.executeUpdate(sql, goodsCount, userId, skuId);
    }

    /**
     * 通过购物车id查询购物车详情
     * @param id 购物车id
     */
    @Override
    public CartItem findById(int id) {
        CartItem c = null;
        String sql = "select * from `t_cart` where id = ?";
        rs = super.executeQuery(sql, id);
        try {
            if (rs.next()) {
                c = new CartItem(rs.getInt("id"), rs.getInt("user_id"), rs.getInt("sku_id"), rs.getInt("goods_count"), rs.getInt("selected"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return c;
    }

    /**
     * 根据购物车id更新购物车中的购物数量
     * @param id 购物车id
     * @param goodsCount 购买数量
     */
    @Override
    public int update(int id,int goodsCount) {
        String sql = "update `t_cart` set `goods_count` = ? where `id` = ?";
        return super.executeUpdate(sql, goodsCount, id);
    }

    /**
     * 批量删除购物车信息
     * @param ids 购物车id，多个id值是使用逗号隔开
     */
    @Override
    public int batchDel(String ids) {
        String sql = "delete from `t_cart` where FIND_IN_SET(id, ?)";
        return super.executeUpdate(sql, ids);
    }

    /**
     * 查询当前登录用户购物车中的商品列表
     * @param userId 当前登录的微信用户id
     */
    @Override
    public List<CartItem> findByUserId(int userId) {
        List<CartItem> list = new ArrayList<>();
        String sql = "select * from `t_cart` where user_id = ?";
        rs = super.executeQuery(sql, userId);
        try {
            while (rs.next()) {
                CartItem c = new CartItem(rs.getInt("id"),rs.getInt("user_id"), rs.getInt("sku_id"), rs.getInt("goods_count"),rs.getInt("selected"));
                list.add(c);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return list;
    }

    /**
     * 购物车中的单选操作
     * @param id 购物车id
     * @param selected 单选状态：0表示false未勾选，1表示true已勾选
     * @return
     */
    @Override
    public int updateSelected(int id, int selected) {
        String sql = "update `t_cart` set `selected` = ? where `id` = ?";
        return super.executeUpdate(sql, selected, id);
    }

    /**
     * 修改当前登录用户的购物车列表选中状态为全选或全不选
     * @param userId 登录用户的id
     * @param allSelected 选中状态：0表示false未勾选，1表示true已勾选
     */
    @Override
    public int updateAllSelect(int userId, int allSelected) {
        String sql = "update `t_cart` set `selected` = ? where `user_id` = ?";
        return super.executeUpdate(sql, allSelected, userId);
    }

    /**
     * 查询当前登录用户购物车中被选中的商品列表
     * @param userId 当前登录的微信用户id
     */
    @Override
    public List<CartItem> findSelectedByUserId(int userId) {
        List<CartItem> list = new ArrayList<>();
        String sql = "select * from `t_cart` where user_id = ? AND selected = 1";
        rs = super.executeQuery(sql, userId);
        try {
            while (rs.next()) {
                CartItem c = new CartItem(rs.getInt("id"),rs.getInt("user_id"), rs.getInt("sku_id"), rs.getInt("goods_count"),rs.getInt("selected"));
                list.add(c);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return list;
    }
}
